package com.arangodb.arangodemo.service;

import com.arangodb.arangodemo.arangoEntity.UgcArticle;
import com.arangodb.arangodemo.arangoEntity.UserInfo;
import com.arangodb.arangodemo.reposity.UgcArticleRepository;
import com.arangodb.arangodemo.reposity.UserInfoRepository;
import com.arangodb.arangodemo.util.RandomDateTime;
import com.arangodb.arangodemo.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UgcArticleService {
    @Autowired
    private UgcArticleRepository ugcArticleRepository;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private ArticleInfoService articleInfoService;

    public void save() {
        Iterable<UserInfo> user = userInfoRepository.findAll();
        List<UserInfo> list = new ArrayList<>();
        user.forEach(userInfo -> {
            list.add(userInfo);
        });
        Integer index = (int) (Math.random() * (list.size()));
        UgcArticle ugcArticle = new UgcArticle();
        ugcArticle.setUserId(list.get(index).getUserId());
        ugcArticle.setCreateTime(RandomDateTime.randomDate("2020-11-01", "2020-12-05").getTime());
        String articleId = String.valueOf(new SnowFlake(1, 1, 1).nextId());
        ugcArticle.setArticleId(articleId);
        ugcArticle.setArtId(articleId);
        ugcArticle.setBan((int) (Math.random() * 2));
        ugcArticle.setLike((long) (Math.random() * 100 + 1200));

        ugcArticleRepository.save(ugcArticle);
        Integer count = (int)(Math.random()*2);
        if(count == 1){
            articleInfoService.save(articleId);
        }


    }
}
